package com.yc.bestMatching;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

public class BestMatchingTask implements Callable<BestMatchingData> {

    private List<String> words;
    private String word;
    private int startIndex;
    private int endIndex;

    public BestMatchingTask(List<String> words, String word, int startIndex, int endIndex) {
        this.words = words;
        this.word = word;
        this.startIndex = startIndex;
        this.endIndex = endIndex;
    }

    @Override
    public BestMatchingData call() throws Exception {
        List<String> minDistanceWords=new ArrayList<>();//最短距离 的单词列表
        int minDistance=Integer.MAX_VALUE;
        int distance; //计算的距离

        //循环 words集合 从startIndex到endIndex 计算 word与这些单词之间的编辑距离
        for(int i=startIndex;i<endIndex;i++){
            //记录最小的编辑距离 及他对应的单词列表
            distance=EditDistance.calculate(word,words.get(i));
            //找到更短距离的单词
            if(distance<minDistance){
                minDistanceWords.clear();  //先清空 集合
                minDistance=distance;
                minDistanceWords.add(words.get(i));  //存这个新的最短距离 对应的单词列表
            }else if(distance==minDistance){
                minDistanceWords.add(words.get(i));  // 如果这个单词与原来的最短距离 单词一样距离 直接存
            }
        }
        Thread.sleep(2000);
        //将最小的编辑距离  及从单词列表包装成BestMatchingData对象返回
        BestMatchingData result=new BestMatchingData();
        result.setDistance(minDistance);
        result.setWords(minDistanceWords);

        return result;
    }
}
